/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ipccenter.beans;

import ru.ipccenter.entities.Human;
import java.sql.Timestamp;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

/**
 *
 * @author sm-ni_000
 */
@Stateless
public class HumanBean implements HumanBeanLocal {
    
    @PersistenceContext(unitName = "WarehousePU", type = PersistenceContextType.TRANSACTION)
    private EntityManager em;

    @Override
    public void create(Human entity) {
        em.persist(entity);     
    }

    @Override
    public void save(Human entity) {
        em.merge(entity);
    }
    
    @Override
    public void delete(Human entity) {
        em.remove(entity);
    }
    
    @Override
    public Human find(Object id) {
        return em.find(Human.class, id);
    }

    @Override
    public List<Human> findByName(String firstName, String lastName) {
        return em.createNamedQuery("Human.findByName", Human.class)
		            .setParameter("firstName", firstName).setParameter("lastName", lastName).getResultList();
    }

    @Override
    public List<Human> findByDate(Timestamp beginDate, Timestamp endDate) {
        return em.createNamedQuery("Human.findByDate", Human.class)
		            .setParameter("beginDate", beginDate).setParameter("endDate", endDate).getResultList();
    }

    @Override
    public List<Human> findAll() {
      return em.createNamedQuery("Human.findAll", Human.class).getResultList();
    }

    @Override
    public Human findByLogin(String login) {
        return em.createNamedQuery("Human.findByLogin", Human.class)
                            .setParameter("login", login).getSingleResult();
                
    }
    
}
